package com.gxa.community.admin.controller.goods;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxa.community.admin.service.goods.impl.GoodsBrandServiceImpl;
import com.gxa.community.core.pojo.domain.goodsdo.GoodsBrand;
import com.gxa.community.core.utils.R;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 商品品牌 前端控制器
 * </p>
 *
 * @author gxa
 * @since 2022-07-18
 */
@RestController
@Validated
@RequestMapping("/goods/brand")
public class GoodsBrandController {
    @Autowired
    GoodsBrandServiceImpl goodsBrandService;


    /**
     * 添加品牌
     * @param goodsBrand
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody GoodsBrand goodsBrand) {
        boolean b = goodsBrandService.saveOrUpdate(goodsBrand);
        if (b){
            return R.ok();
        }
        return R.error();
    }

    /**
     * 获取数据并分页
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/query")
    public R query(@NotNull(message = "page不能为空") Integer page,@NotNull(message = "size不能为空") Integer size) {
        Page<GoodsBrand> Page = new Page<>(page,size);
        var page1 = goodsBrandService.page(Page, null);
        return R.ok().put("data",page1.getRecords()).put("cont",page1.getTotal());
    }
    /**
     * 修改品牌
     * @param goodsBrand
     * @return
     */
    @PutMapping("/edit")
    public R edit(@RequestBody GoodsBrand goodsBrand) {
        boolean b = goodsBrandService.updateById(goodsBrand);
        if (b){
            return R.ok();
        }
        return R.error();
    }
    /**
     * 批量删除品牌
     * @param id
     * @return
     */
    @DeleteMapping("/del")
    public R del(@NotNull(message = "不能为空") Integer... id) {
        Integer count = 0;
        for (Integer integer : id) {
            boolean b = goodsBrandService.removeById(integer);
            if (b){
                count++;
            }
        }
        return R.ok("成功删除了:"+count+"个");
    }
    /**
     * 根据品牌名字查询
     * @param name
     * @return
     */
    @GetMapping("/seek")
    public R seek(@NotEmpty(message = "搜索内容不能为空") String name, @NotNull(message = "page不能为空") Integer page, @NotNull(message = "size不能为空") Integer size) {
        QueryWrapper <GoodsBrand> wrapper1 = new QueryWrapper<GoodsBrand>();
        wrapper1.eq("name",name);
        Page<GoodsBrand> Page = new Page<>(page,size);
        var page1 = goodsBrandService.page(Page, wrapper1);
        //把分页完成的数据和总共的数量返回
        return R.ok().put("data",page1.getRecords()).put("cont",Page.getTotal());
    }
    /**
     * 修改品牌状态
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/editStatus")
    public R editStatus(@NotNull(message = "id不能为空") Integer id,@NotNull(message = "状态码不能为空") Integer status) {
        UpdateWrapper wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        wrapper.set("status",status);
        boolean b = goodsBrandService.update(null, wrapper);
        if (b){
            return R.ok();
        }
        return R.error();
    }

}

